package com.dimong.www.loan.ui.act;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dimong.loan.sdk.net.NetHttpCallback;
import com.dimong.www.loan.R;
import com.dimong.www.loan.app.MyApplication;
import com.dimong.www.loan.bean.ProdBean;
import com.dimong.www.loan.bean.ProdDetailedBean;
import com.dimong.www.loan.net.LoadHttpUtil;
import com.dimong.www.loan.ui.UIHelper;
import com.dimong.www.loan.ui.base.BaseRefreshActivity;
import com.dimong.loan.sdk.utils.GlideUtil;
import com.dimong.www.loan.utils.ToolUtil;
import com.dimong.www.loan.widget.ActionBarView;
import com.dimong.www.loan.widget.LoanRefreshView;
import com.dimong.www.loan.widget.LoanSelectPopWindow;

/**
 * Created by wuligang on 16/8/30.
 */
public class ProdDetailedAty extends BaseRefreshActivity implements View.OnClickListener, LoanSelectPopWindow.LoanSelectCallback {
    private View rl_act_loan_detail;
    private View rl_act_loan_detail_data;
    private ActionBarView title_act_loan_detail;
    private ImageView iv_prod_pic;
    private TextView tv_prod_name;
    private TextView tv_prod_apply_num;
    private TextView tv_prod_tag;
    private TextView tv_prod_detail_limit;
    private TextView tv_prod_detail_rate;
    private TextView tv_prod_detail_lending_time;
    private TextView tv_prod_detail_deadline;
    private TextView tv_prod_detail_apply;
    private String prod_id;
    private View bt_loan_commit;
    private TextView tv_prod_money;
    private View rl_prod_deadline_select;
    private TextView tv_prod_deadline_select;

    private ProdDetailedBean prodDetailedBean;
    private LoanSelectPopWindow loanSelectPopWindow;

    @Override
    public void initIntentParam(Intent intent) {
    }

    @Override
    public void beforeInitView() {

    }

    @Override
    public void initView() {
        super.initView();
        rl_act_loan_detail = findViewById(R.id.rl_act_loan_detail);
        title_act_loan_detail = findViewById(R.id.title_act_loan_detail);
        rl_act_loan_detail_data = findViewById(R.id.rl_act_loan_detail_data);
        iv_prod_pic = findViewById(R.id.iv_prod_pic);
        tv_prod_name = findViewById(R.id.tv_prod_name);
        tv_prod_apply_num = findViewById(R.id.tv_prod_apply_num);
        tv_prod_tag = findViewById(R.id.tv_prod_tag);
        tv_prod_detail_limit = findViewById(R.id.tv_prod_detail_limit);
        tv_prod_detail_rate = findViewById(R.id.tv_prod_detail_rate);
        tv_prod_detail_lending_time = findViewById(R.id.tv_prod_detail_lending_time);
        tv_prod_detail_deadline = findViewById(R.id.tv_prod_detail_deadline);
        tv_prod_detail_apply = findViewById(R.id.tv_prod_detail_apply);
        bt_loan_commit = findViewById(R.id.bt_loan_commit);
        tv_prod_money = findViewById(R.id.tv_prod_money);
        rl_prod_deadline_select = findViewById(R.id.rl_prod_deadline_select);
        tv_prod_deadline_select = findViewById(R.id.tv_prod_deadline_select);
    }

    @Override
    public void setViewStatus() {
        rl_prod_deadline_select.setOnClickListener(this);
        prod_id = getIntent().getStringExtra("prod_id");
        if (prod_id == null) {
            finish();
        }
        title_act_loan_detail.setTitle("平台详情");
        title_act_loan_detail.setLeftImg(new ActionBarView.OnLeftImgClickListener() {
            @Override
            public void onClick(View button) {
                finish();
            }
        });

        bt_loan_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MyApplication.isLogin()) {
                    String money = tv_prod_deadline_select.getText().toString();
                    try {
                        int after = ToolUtil.passInt(money);
                        int min = ToolUtil.passInt(prodDetailedBean.getLimit_min());
                        int max = ToolUtil.passInt(prodDetailedBean.getLimit_max());
                        if (after < min || after > max) {
                            showToast("输入金额不正确");
                            return;
                        }
                    } catch (Exception e) {
                        money = tv_prod_deadline_select.getText().toString();
                    }

                    LoadHttpUtil.subLoanLog("2", prod_id, money, tv_prod_deadline_select.getText().toString(), new NetHttpCallback() {
                        @Override
                        public void onPreRequest() {
                            super.onPreRequest();
                            showRefreshLoading(true);
                        }

                        @Override
                        public void onResult(String result) {
                            super.onResult(result);
                            showRefreshSuccess();
                            ProdBean prodBean = gson.fromJson(result, ProdBean.class);
                            UIHelper.openWebViewAty(ProdDetailedAty.this, prodBean.getLoan_name(), prodBean.getLink());
                        }

                        @Override
                        public void onError(String errorCode, String error) {
                            showRefreshSuccess();
                            showToast(error);
                            super.onError(errorCode, error);
                        }
                    });
                } else {
                    MyApplication.openLogin();
                }
            }
        });
        rl_act_loan_detail_data.setVisibility(View.GONE);
        showRefreshLoading();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getData();
            }
        }, 1000);

        setLoanReloadClick(new LoanRefreshView.LoanReloadClick() {
            @Override
            public void onRefreshClick() {
                rl_act_loan_detail_data.setVisibility(View.GONE);
                showRefreshLoading();
                getData();
            }
        });
    }

    private void getData() {
        LoadHttpUtil.getProdDetailed(prod_id, new NetHttpCallback() {
            @Override
            public void onPreRequest() {
                super.onPreRequest();
            }

            @Override
            public void onResult(String result) {
                super.onResult(result);
                prodDetailedBean = gson.fromJson(result, ProdDetailedBean.class);
                showRefreshSuccess();
                rl_act_loan_detail_data.setVisibility(View.VISIBLE);
                setData();
            }

            @Override
            public void onError(String errorCode, String error) {
                super.onError(errorCode, error);
                showRefreshError();
                showToast(error);
            }
        });
    }

    private void setData() {
        GlideUtil.loadImg(this, prodDetailedBean.getLogo(), iv_prod_pic);
        tv_prod_name.setText(prodDetailedBean.getLoan_name());
        tv_prod_apply_num.setText(prodDetailedBean.getApply_num());
        tv_prod_tag.setText(prodDetailedBean.getTag());
        tv_prod_detail_limit.setText("可贷：" + prodDetailedBean.getLimit_str());
        tv_prod_detail_rate.setText("参考利率：" + prodDetailedBean.getRate_str());
        tv_prod_detail_lending_time.setText("最快放款时间：" + prodDetailedBean.getLending_time_str());
        tv_prod_detail_deadline.setText("借款期限：" + prodDetailedBean.getDeadline_str());
        if (prodDetailedBean.getApply() != null) {
            StringBuffer str = new StringBuffer();
            for (int i = 0; i < prodDetailedBean.getApply().size(); i++) {
                str.append(prodDetailedBean.getApply().get(i));
                str.append("\n");
            }
            tv_prod_detail_apply.setText(str);
        }

        tv_prod_money.setText(prodDetailedBean.getInput());
        tv_prod_deadline_select.setText(prodDetailedBean.getDeadline_select().get(0));
    }

    @Override
    public void onActResume() {

    }

    @Override
    public void onActPause() {

    }

    @Override
    public void onActDestroy() {

    }

    @Override
    public void onActRestart() {

    }

    @Override
    public void onActStop() {

    }

    @Override
    public void onActStart() {

    }

    @Override
    public int getContentView() {
        return R.layout.act_loan_detail;
    }

    @Override
    public RelativeLayout getContainView() {
        return findViewById(R.id.rl_act_loan_detail);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_prod_deadline_select:
                if (loanSelectPopWindow == null) {
                    loanSelectPopWindow = new LoanSelectPopWindow(ProdDetailedAty.this);
                    loanSelectPopWindow.setLoanSelectCallback(this);
                }
                loanSelectPopWindow.showPopupWindow(rl_act_loan_detail, prodDetailedBean.getDeadline_select());
                break;
        }
    }

    @Override
    public void loanSelectClick(int viewPosition) {
        tv_prod_deadline_select.setText(prodDetailedBean.getDeadline_select().get(viewPosition));
    }
}